package com.sxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxl.domain.Modules;
import com.sxl.domain.Rolemodules;
import com.sxl.domain.UserModule;
import com.sxl.service.ModulesService;
import com.sxl.service.RolemodulesService;
import com.sxl.service.UserModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/8/15 18:45
 */
@RestController
@CrossOrigin
@RequestMapping("/userModule")
public class UserModuleController {
    @Autowired
    UserModuleService userModuleService;
    @Autowired
    private ModulesService modulesService;
    @Autowired
    private RolemodulesService rolemodulesService;
    /**
     * 树状图
     */
    @PostMapping("/getDendrogram")
    public List<Modules> getMenu(Integer userId){
        //回显当前角色的权限
        QueryWrapper<UserModule> wrapper=new QueryWrapper<>();
        wrapper.eq("users_id",userId);
        List<UserModule> currList=userModuleService.list(wrapper);

        //1、查询所有菜单数据
        List<Modules> allMenu=modulesService.list();
        //2、获取所有的一级菜单
        List<Modules> rootMenu=new ArrayList<>();
        for(Modules m:allMenu){
            if (m.getParentId()==-1){
                rootMenu.add(m);
            }
        }
        //3、设置一级菜单的子菜单
        for(Modules m: rootMenu){
            //调用递归
            m.setChildren(getChildren(m.getId(),allMenu,currList));
            //已有的权限设置勾选
            for (UserModule rm : currList){
                if (m.getId().equals(rm.getModulesId())){
                    m.setChecked(true);
                    m.setSpread(true);
                }
            }
        }
        return rootMenu;
    }
    /**
     * 递归查找子菜单
     * @param pid
     * @param all
     * @return
     */
    private List<Modules> getChildren(Integer pid,List<Modules> all, List<UserModule> currList){
        List<Modules> children=new ArrayList<>();//子菜单集合
        //、在所有菜单里面找父id=pid的
        for (Modules m:all){
            if(m.getParentId().equals(pid)){
                children.add(m);
            }
        }
        //2、递归找子的子....
        for (Modules m:children){
            m.setChildren(getChildren(m.getId(),all,currList));
            //已有权限设置勾选
            for (UserModule rm : currList){
                if (m.getId().equals(rm.getModulesId())){
                    m.setChecked(true);
                    m.setSpread(true);
                }
            }
        }
        //3、递归退出
        if (children.size()==0){
            return new ArrayList<>();
        }
        return  children;
    }
    /**
     * 角色管理 保存权限
     * @param roleId  当前角色id
     * @param selectedModule
     * @return
     */
    @PostMapping("/savePermission")
    public Boolean savePermission(Integer roleId,@RequestParam("selectedModule") List<Integer> selectedModule){
        boolean remove = userModuleService.remove(new LambdaQueryWrapper<UserModule>().eq(UserModule::getUsersId,roleId));
        if(selectedModule.size()>0){
            List<UserModule> batchSaveList = new ArrayList<>();
            for (Integer mid : selectedModule){
                UserModule r = new UserModule();
                r.setUsersId(roleId);
                r.setModulesId(mid);
                batchSaveList.add(r);
            }
            return userModuleService.saveBatch(batchSaveList);
        }
        return remove;
    }
}
